package com.cloudptt.api.product.func.media.device;

import android.content.Intent;
import android.content.IntentFilter;

import com.cloudptt.api.comm.Const;
import com.cloudptt.api.product.func.media.EventListener;
import com.cloudptt.api.product.log.Log;

public class DtYellow implements IDeviceKey {
	private static final String BRAND = "Android";
	private static final String DEVICE = "Android";
	private static final String MODEL = "Android";

	private IntentFilter mFilter;

	/**指环PTT**/
//	 private final static String ACTION_PTT = "com.qcomm.qchat.PTT_CHANGE_ACTION";
	private final static String ACTION_PTT_DOWN = "android.intent.action.PTT.down";
	private final static String ACTION_PTT_UP = "android.intent.action.PTT.up";


	public DtYellow(){
		
	}
	
	public static boolean isThisDev(String brand,String device,String model){
		if(brand == null
				|| device == null
				|| model == null){
			return false;
		}
		else if(BRAND.equals(brand)
				&& DEVICE.equals(device)
				&& MODEL.equals(model)){
			return true;
		}
		return false;
	}

	@Override
	public IntentFilter getBstIntentFilter() {
		mFilter = new IntentFilter();

		mFilter.addAction(ACTION_PTT_DOWN);
		mFilter.addAction(ACTION_PTT_UP);
		
		return mFilter;
	}
	
	@Override
	public void procBst(Intent intent, EventListener listener) {
		String action = intent.getAction();
		Log.debug(this.getClass(), " procBst action = " + action + " intent = " + intent);
		if (intent != null)
		{
			Log.debug(this.getClass(), " procBst intent.getExtras() = " + intent.getExtras());
		}
		if(action.equals(ACTION_PTT_DOWN)){
			Log.debug(this.getClass(), " ACTION_PTT_DOWN");
			if (listener != null) {
				listener.onPtt(Const.MSG_TALK_ON,false,false,true,false);
			}
		}else if(action.equals(ACTION_PTT_UP)){
			Log.debug(this.getClass(), " ACTION_PTT_UP");
			if (listener != null) {
				listener.onPtt(Const.MSG_TALK_OFF,false,false,true,false);
			}
		}
	}
	
	/** 解析登录信息包 **/
	public static final int MSG_HEAD_LENGTH = 2 * 4;
	public static final int MSG_NAME_LENGTH = 8 * 4;
	public static final int MSG_PSW_LENGTH = 8 * 4;
	public static final int MSG_IP_LENGTH = 4 * 4;
	public static final int MSG_PORT_LENGTH = 4;

	public String parseUserName(byte[] msg) {
		if(msg == null || msg.length == 0)return null;
		int offset = MSG_HEAD_LENGTH;
		byte[] data = new byte[MSG_NAME_LENGTH];
		System.arraycopy(msg, offset, data, 0, MSG_NAME_LENGTH);
		int len = 0;
		for(int i= 0;i <MSG_NAME_LENGTH;i++){
			if(data[i] !=0){
				len =len+1;
			}
		}
		if(len!=0){
			byte[] resultdata = new byte[len];
			System.arraycopy(data, 0, resultdata, 0, len);
			return new String(resultdata);
		}
		return "";
	}
	
	public String parsePassword(byte[] msg) {
		if(msg == null || msg.length == 0)return null;
		int offset = MSG_HEAD_LENGTH + MSG_NAME_LENGTH;
		byte[] data = new byte[MSG_PSW_LENGTH];
		System.arraycopy(msg, offset, data, 0, MSG_PSW_LENGTH);
		
		int len = 0;
		for(int i= 0;i <MSG_PSW_LENGTH;i++){
			if(data[i] !=0){
				len =len+1;
			}
		}
		if(len!=0){
			byte[] resultdata = new byte[len];
			System.arraycopy(data, 0, resultdata, 0, len);
			return new String(resultdata);
		}
		return "";
	}
	
	public String parseServerIP(byte[] msg) {
		if(msg == null || msg.length == 0)return null;
		int offset = MSG_HEAD_LENGTH + MSG_NAME_LENGTH + MSG_PSW_LENGTH;
		byte[] data = new byte[MSG_IP_LENGTH];
		System.arraycopy(msg, offset, data, 0, MSG_IP_LENGTH);
		
		int len = 0;
		for(int i= 0;i <MSG_IP_LENGTH;i++){
			if(data[i] !=0){
				len =len+1;
			}
		}
		if(len!=0){
			byte[] resultdata = new byte[len];
			System.arraycopy(data, 0, resultdata, 0, len);
			return new String(resultdata);
		}
		return "";
	}
	
	public int parseServerPort(byte[] msg) {
		if(msg == null || msg.length == 0)return 0;
		int offset = MSG_HEAD_LENGTH + MSG_NAME_LENGTH + MSG_PSW_LENGTH + MSG_IP_LENGTH;
		byte[] data = new byte[MSG_PORT_LENGTH];
		System.arraycopy(msg, offset, data, 0, MSG_PORT_LENGTH);
		int port = byteArrayToInt(data);
		return port;
	}
	
	public int byteArrayToInt(byte[] b) {
		byte[] a = new byte[4];
		int i = a.length - 1;
		int j = b.length - 1;
		for (; i >= 0; i--, j--) {
			if (j >= 0) {
				a[i] = b[j];
			} else {
				a[i] = 0;
			}
		}

		// litter ending
		// int v0 = (a[0] & 0xFF) << 24;
		// int v1 = (a[1] & 0xFF) << 16;
		// int v2 = (a[2] & 0xFF) << 8;
		// int v3 = (a[3] & 0xFF);

		// big ending
		int v0 = (a[0] & 0xFF);
		int v1 = (a[1] & 0xFF) << 8;
		int v2 = (a[2] & 0xFF) << 16;
		int v3 = (a[3] & 0xFF) << 24;
		return v0 + v1 + v2 + v3;
	}

}
